﻿//第一版在frag()操作uv，太线性，也无法实现云效果，改良一下，版本2
Shader "Custom/NewUnlitCloud2"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_MainColor ("Main Color", Color) = (1,1,1,1)
		_Speed("Rotate Speed",Range(0,4))=1
		_Cutoff("Gray Factor",Range(-1,1))=0.2
		_midYValue("_MidYValue",float)=1
		_cloudHeight("_CloudHeight",float)=5
		_TaperPower("_TaperPower",float)=3
		_CloudSize("_CloudSize",Range(0.01,3))=0.2
		_LitStrength("_LitStrength",Range(0.1,20))=5
		_BackLitStrength ("_BackLitStrength", range (0.1,20)) = 5
		 _EdgeLitPower ("Edge Power", range (0.1,10)) = 1
		_EdgeLitStrength("_EdgeLitStrength",range(0.1,20))=5
		_TestBar("Test",Range(0,1))=0.2
		_OffsetDistance("_OffsetDistance",Range(0,1))=0.1
	}
	SubShader
	{
		//Tags { "RenderType"="Opaque" }
		//Tags { "RenderType"="Transparent" }
		Tags { "RenderType"="Opaque" "LightMode"="ForwardBase"}
		LOD 100

		Pass
		{
		    Cull Off
		    //Blend SrcAlpha OneMinusSrcAlpha
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag			
			#include "UnityCG.cginc"
            #include "Lighting.cginc"
			sampler2D _MainTex;
			float4 _MainTex_ST;
			float _Speed;
            float _Cutoff;
            float _midYValue;
            float _cloudHeight;
            float _TaperPower;
            float _CloudSize;
            float _LitStrength;
            float _BackLitStrength;
            float _EdgeLitPower;
            float _EdgeLitStrength;
            float4 _MainColor;
            float _TestBar;
            float _OffsetDistance;
			struct a2v{
				float4 vertex:POSITION;
				float4 texcoord:TEXCOORD;
				float4 tangent:TANGENT;
			};
			struct v2f{
				float4 pos:SV_POSITION;
				float2 uv1:texcoord;
				float2 uv2:texcoord1;
				float4 posWorld:TEXCOORD2;
				float3 lightDir:TEXCOORD3;
				float3 viewDir:TEXCOORD4;
				float3 tangentWorld:TEXCOORD5;
				float2 uvOffset1:TEXCOORD6;
				float2 uvOffset2:TEXCOORD7;
				float2 uvOffset3:TEXCOORD8;
				float2 uvOffset4:TEXCOORD9;
			};

			v2f vert(a2v v){
				v2f o;
				o.pos=UnityObjectToClipPos(v.vertex);
				o.posWorld = mul(unity_ObjectToWorld, v.vertex);
				float2 uv =TRANSFORM_TEX(v.texcoord,_MainTex);
			
				float2 uvPanner1 = uv + _Speed * float2 ( 1+0.02,  0+0.02) * _Time ;
                float2 uvPanner2 = uv + _Speed * float2 ( 1-0.02,  0-0.02) * _Time ;
                o.uv1 = uvPanner1 * (_CloudSize + 0.13);
                o.uv2 = uvPanner2 * _CloudSize;
				
				
				o.lightDir = WorldSpaceLightDir(v.vertex);
				o.viewDir = WorldSpaceViewDir(v.vertex);
				o.tangentWorld = UnityObjectToWorldDir(v.tangent.xyz);
				
//				//offset
//				float3 litOffset = o.lightDir * o.tangentWorld * _OffsetDistance;
//				o.uvOffset1 = o.uv1 + litOffset;
//				o.uvOffset2 = o.uv2 + litOffset;
//				o.uvOffset3 = o.uv1 - litOffset;
//				o.uvOffset4 = o.uv2 - litOffset;
    // offset
                float3 litOffset = o.lightDir * o.tangentWorld * _OffsetDistance;
                o.uvOffset1 = o.uv1 + litOffset;
                o.uvOffset2 = o.uv2 + litOffset;

                o.uvOffset3 = o.uv1 - litOffset;
                o.uvOffset4 = o.uv2 - litOffset;

				return o;
			}

	/*		fixed4 frag(v2f i):SV_Target{
				
				
				fixed4 texCol1 = tex2D(_MainTex, i.uv1);
				fixed4 texCol2 = tex2D(_MainTex,i.uv2);
			    			    
				fixed4 col = texCol1 * texCol2;
				float vFalloff = pow(saturate (abs(_midYValue - i.posWorld.y) / (_cloudHeight * 0.25)), _TaperPower);

//		          // get light value

                fixed4 texColOffset1 = tex2D(_MainTex, i.uvOffset1);
                fixed4 texColOffset2 = tex2D(_MainTex, i.uvOffset2);
                fixed light = saturate (texCol1.r + texCol2.r - texColOffset1.r - texColOffset2.r) * _LitStrength;
                //fixed light = saturate (texCol1.r + texCol2.r) * _LitStrength;//必须带offset,否则黑色
                // backlight value
                fixed4 texColOffset3 = tex2D(_MainTex, i.uvOffset3);
                fixed4 texColOffset4 = tex2D(_MainTex, i.uvOffset4);
                fixed backLight = saturate (texCol1.r + texCol2.r - texColOffset3.r - texColOffset4.r) * _BackLitStrength;
                //fixed backLight = saturate (texCol1.r + texCol2.r) * _BackLitStrength;//必须带offset,否则黑色

                fixed edgeLight = pow((1 - col.r), _EdgeLitPower) * _EdgeLitStrength;

                fixed finalLit = light + backLight + edgeLight;
		        
//		        fixed4 texColOffset1 = tex2D(_MainTex, i.uvOffset1);
//                fixed4 texColOffset2 = tex2D(_MainTex, i.uvOffset2);
//		        fixed light = saturate (texCol1.r + texCol2.r - texColOffset1.r - texColOffset2.r) * _LitStrength;
//		         fixed4 texColOffset3 = tex2D(_MainTex, i.uvOffset3);
//                fixed4 texColOffset4 = tex2D(_MainTex, i.uvOffset4);
//		        fixed backLigt = saturate(texCol1.r + texCol2.r-texColOffset3.r - texColOffset4.r) * _BackLitStrength;
//		        fixed edgeLight = pow((1-col.r),_EdgeLitPower)*_EdgeLitStrength;
//		        fixed finalLit = light + backLigt + edgeLight;

		        clip(col.r -vFalloff -_Cutoff);
		        fixed4 finalCol = lerp(_MainColor,_LightColor0,finalLit);

				return finalCol;


			}*/

            fixed4 frag (v2f i) : SV_Target
            {

                // sample the texture
                fixed4 texCol1 = tex2D(_MainTex, i.uv1);
                fixed4 texCol2 = tex2D(_MainTex, i.uv2);
                fixed4 col = texCol1 * texCol2;

                // get light value
//                fixed4 texColOffset1 = tex2D(_MainTex, i.uvOffset1);
//                fixed4 texColOffset2 = tex2D(_MainTex, i.uvOffset2);
//                fixed light = saturate (texCol1.r + texCol2.r - texColOffset1.r - texColOffset2.r) * _LitStrength;
  // get light value
              fixed4 texColOffset1 = tex2D(_MainTex, i.uvOffset1);
               fixed4 texColOffset2 = tex2D(_MainTex, i.uvOffset2);
              fixed light = saturate (texCol1.r + texCol2.r - texColOffset1.r - texColOffset2.r) * _LitStrength;

                //fixed light = saturate (texCol1.r + texCol2.r) * _LitStrength;//必须带offset,否则黑色
//                // backlight value
//                fixed4 texColOffset3 = tex2D(_MainTex, i.uvOffset3);
//                fixed4 texColOffset4 = tex2D(_MainTex, i.uvOffset4);
//                fixed backLight = saturate (texCol1.r + texCol2.r - texColOffset3.r - texColOffset4.r) * _BackLitStrength;
 // backlight value
                fixed4 texColOffset3 = tex2D(_MainTex, i.uvOffset3);
                fixed4 texColOffset4 = tex2D(_MainTex, i.uvOffset4);
                fixed backLight = saturate (texCol1.r + texCol2.r - texColOffset3.r - texColOffset4.r) * _BackLitStrength;
                //fixed backLight = saturate (texCol1.r + texCol2.r) * _BackLitStrength;//必须带offset,否则黑色

                //fixed edgeLight = pow((1 - col.r), _EdgeLitPower) * _EdgeLitStrength;
                fixed edgeLight = pow((1 - col.r), _EdgeLitPower) * _EdgeLitStrength;

                //fixed finalLit = light + backLight + edgeLight;
                fixed finalLit = light + backLight + edgeLight;

                // get value to taper top and bottom
                float vFalloff = pow(saturate (abs(_midYValue - i.posWorld.y) / (_cloudHeight * 0.25)), _TaperPower);

                // Subsurface Scatterting
                //fixed3 sssCol = pow(saturate(dot(i.viewDir, i.lightDir)), _SssPower) * _SssStrength * _LightColor0;

                clip(col.r - vFalloff - _Cutoff);

                // get cloud color
                fixed4 finalCol = lerp(_MainColor, _LightColor0, finalLit);
      
                // apply fog
                UNITY_APPLY_FOG(i.fogCoord, finalCol);

                return finalCol;
            }
            ENDCG
        }


	}
}

